<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:py="http://genshi.edgewall.org/"
      xmlns:i18n="http://genshi.edgewall.org/i18n"
      xmlns:xi="http://www.w3.org/2001/XInclude">
  <xi:include href="layout.html" />
  <head>
    <title>$title</title>
    <script type="text/javascript" src="${chrome.htdocs_location}js/folding.js"></script>
    <script type="text/javascript">/*<![CDATA[*/
      jQuery(document).ready(function($) {
        initializeFilters();
        $("#group").change(function() {
          $("#groupdesc").enable(this.selectedIndex != 0)
        }).change();
        $("fieldset legend.foldable").enableFolding(false);
        /* Hide the filters for saved queries. */
        if (window.location.href.search(/[?&]report=[0-9]+/) != -1)
          $("#filters").toggleClass("collapsed");
        /* Hide the columns by default. */
        $("#columns").toggleClass("collapsed");
      });
    /*]]>*/</script>
  </head>

  <body>
    <div id="content" class="query">
      <h1>$title <span class="numrows">(${ngettext('%(num)s match', '%(num)s matches', query.num_items)})</span></h1>

      <div py:if="description" id="description" xml:space="preserve">
        ${wiki_to_html(context(report_resource), description)}
      </div>

      <form id="query" method="post" action="${href.query()}"
            py:with="field_names = sorted(fields.iterkeys(), key=lambda name: fields[name].label.lower())">
        <fieldset id="filters">
          <legend class="foldable">Filters</legend>
          <table summary="Query filters">
            <tbody py:for="clause_num, constraints in enumerate(clauses or [{}])"
                   py:with="clause_pre = '%d_' % clause_num">
              <tr style="${clause_num == 0 and 'display: none' or None}">
                <td>
		  <div class="trac-clause-lsep">&nbsp;<hr /></div>
		  <div class="trac-clause-msep">Or</div>
		  <div class="trac-clause-rsep">&nbsp;<hr /></div>
		</td>
              </tr>
              <tr><td class="trac-clause">
                <table class="trac-clause">
                  <tbody py:for="field_name in field_names" py:if="field_name in constraints"
                         py:with="field = fields[field_name]; n_field_name = clause_pre + field_name;
                                  constraint = constraints[field_name];
                                  multiline = field.type in ('select', 'text', 'textarea', 'time')">
                    <tr py:for="constraint_idx, constraint_value in enumerate(constraint['values'])"
                        class="${field_name}" py:if="multiline or constraint_idx == 0">
                      <td>
                        <div class="inlinebuttons">
                          <input type="submit" name="rm_filter_${n_field_name}_${constraint_idx}" value="&ndash;" />
                        </div>
                      </td>
                      <py:choose test="constraint_idx">
                        <py:when test="0">
                          <th scope="row"><label id="label_${n_field_name}">${fields[field_name].label}</label></th>
                          <td py:if="field.type not in ('radio', 'checkbox', 'time')" class="mode">
                            <select name="${n_field_name}_mode">
                              <option py:for="mode in modes[field.type]" value="$mode.value"
                                selected="${mode.value == constraint.mode and 'selected' or None}">$mode.name</option>
                            </select>
                          </td>
                        </py:when>
                        <py:otherwise><!--! not the first line of a multiline constraint -->
                          <th colspan="${field.type == 'time' and 1 or 2}"><label>or</label></th>
                        </py:otherwise>
                      </py:choose>

                      <td class="filter" colspan="${field.type in ('radio', 'checkbox', 'time') and 2 or None}"
                          py:choose="">

                        <py:when test="field.type == 'select'">
                          <select name="${n_field_name}">
                            <option></option>
                            <option py:for="option in field.options"
                              selected="${option == constraint_value and 'selected' or None}"
                              value="$option" py:content="option"></option>
                          </select>
                        </py:when>

                        <py:when test="field.type == 'radio'">
                          <py:for each="option in field.options">
                            <input type="checkbox" id="_${n_field_name}_$option" name="${n_field_name}"
                              value="$option"
                              checked="${((constraint['mode'] == '') == (option in constraint['values']))
                                       and 'checked' or None}" />
                            <label for="_${n_field_name}_$option" class="control">${option or 'none'}</label>
                          </py:for>
                        </py:when>

                        <py:when test="field.type == 'checkbox'">
                          <input type="radio" id="_${n_field_name}_on" name="$n_field_name" value="1"
                                 checked="${constraint.mode != '!' or constraint_value == '1' or None}" />
                          <label for="_${n_field_name}_on" class="control">yes</label>
                          <input type="radio" id="_${n_field_name}_off" name="$n_field_name" value="0"
                                 checked="${constraint.mode == '!' or constraint_value != '1' or None}" />
                          <label for="_${n_field_name}_off" class="control">no</label>
                        </py:when>

                        <py:when test="field.type in ('text', 'textarea', 'id')">
                          <input type="text" name="${n_field_name}" value="$constraint_value" size="42" />
                        </py:when>
                        
                        <py:when test="field.type == 'time'"
                                 py:with="(start, end) = '..' in constraint_value and constraint_value.split('..', 1)
                                                         or (constraint_value, '')">
                          <i18n:msg>
                            <label>between</label>
                            <input type="text" name="${n_field_name}" value="$start" size="14" />
                            <label>and</label>
                            <input type="text" name="${n_field_name}_end" value="$end" size="14" />
                          </i18n:msg>
                        </py:when>
                      </td>
                    </tr>
                  </tbody>

                  <tbody py:with="last_clause = clause_num == (len(clauses) or 1) - 1">
                    <tr class="actions">
                      <td class="and" colspan="2">
                        &nbsp;<label for="add_filter_${clause_num}">And</label>&nbsp;
                        <select name="add_filter_${clause_num}" id="add_filter_${clause_num}">
                          <option></option>
                          <option py:for="field_name in field_names" py:with="field = fields[field_name]"
                                  value="$field_name"
                                  disabled="${(field.type in ('radio', 'checkbox', 'id') and
                                               field_name in constraints and
                                               len(constraints[field_name])) or None}">${field.label}</option>
                        </select>
                        <div class="inlinebuttons">
                          <input type="submit" name="add_${clause_num}" value="+" />
                        </div>
                      </td>
                      <td py:if="last_clause" class="or" colspan="2">
                        <label for="add_clause">Or</label>&nbsp;
                        <select name="add_clause_${clause_num + 1}" id="add_clause">
                          <option></option>
                          <option py:for="field_name in field_names" value="$field_name">${fields[field_name].label}</option>
                        </select>
                        <div class="inlinebuttons">
                          <input type="submit" name="add_${clause_num + 1}" value="+" />
                        </div>
                      </td>
                    </tr>
                  </tbody>
                </table>
              </td></tr>
            </tbody>
          </table>
        </fieldset>

        <!--! Allow the user to decide what columns to include in the output of the query -->
        <fieldset id="columns">
          <legend class="foldable">Columns</legend>
          <div>
            <py:for each="column in all_columns">
              <label>
                <input type="checkbox" name="col" value="$column"
                       checked="${any([(value == column) for value in col])
                                  and 'checked' or None}" />
                ${fields.get(column, {'label': column or 'none'}).label}
              </label>
            </py:for>
          </div>
        </fieldset>

        <p class="option">
          <label for="group">Group results by</label>
          <select name="group" id="group">
            <option></option>
            <py:for each="field_name in field_names" py:with="field = fields[field_name]">
              <option py:if="field.type in ('select', 'radio') or field_name in ('owner', 'reporter')"
                      selected="${field_name == query.group or None}"
                      value="${field_name}">${field.label}</option>
            </py:for>
          </select>
          <input type="checkbox" name="groupdesc" id="groupdesc"
                 checked="${query.groupdesc or None}" />
          <label for="groupdesc">descending</label>
        </p>

        <p class="option">
          <!--! Allow the user to decide what rows to include in the output of the query -->
          Show under each result:
          <py:for each="column in all_textareas">
            <label><input type="checkbox" name="row" value="$column"
                checked="${any([(value == column) for value in row]) and 'checked' or None}" />
            ${fields.get(column, {'label': column or 'none'}).label}</label>
          </py:for>
        </p>

        <p class="option">
          <label for="max">Max items per page</label>
          <input type="text" name="max" id="max" size="10" value="${query.max}" />
        </p>

        <div class="buttons">
          <input py:if="report_resource" type="hidden" name="report" value="$report_resource.id" />
          <input type="hidden" name="order" value="$query.order" />
          <input py:if="query.desc" type="hidden" name="desc" value="1" />
          <input type="submit" name="update" value="${_('Update')}" />
        </div>
        <hr />
      </form>

      <xi:include href="query_results.html" />

      <div class="buttons"
           py:with="edit = report_resource and 'REPORT_MODIFY' in perm(report_resource);
                    new = 'REPORT_CREATE' in perm;
                    delete = report_resource and 'REPORT_DELETE' in perm(report_resource)">
        <form py:if="edit" method="get" action="${url_of(report_resource)}">
          <div>
            <input type="hidden" name="action" value="edit" />
            <input type="submit" 
                   title="${_('Edit report {%(id)s} corresponding to this query', id=report_resource.id)}"
                   value="${_('Edit query')}" />
          </div>
        </form>
        <form py:if="new or edit" method="get" action="${edit and url_of(report_resource) or href.report()}">
          <div>
            <input type="hidden" name="action" value="${edit and 'edit' or 'new'}" />
            <input type="hidden" name="query" value="${query.to_string()}" />
            <input type="submit" value="${_('Save query')}"
                   title="${edit and _('Save updated query in report {%(id)s}', id=report_resource.id) or
                                     _('Create new report from current query')}" />
          </div>
        </form>
        <form py:if="delete" method="get" action="${url_of(report_resource)}">
          <div>
            <input type="hidden" name="action" value="delete" />
            <input type="submit"
                   title="${_('Delete report {%(id)s} corresponding to this query', id=report_resource.id)}"
                   value="${_('Delete query')}" />
          </div>
        </form>
      </div>

      <div id="help" i18n:msg="">
        <strong>Note:</strong> See <a href="${href.wiki('TracQuery')}">TracQuery</a>
        for help on using queries.
      </div>

    </div>
  </body>
</html>

